<?php

/**
 * This is the model class for table "testimony".
 *
 * The followings are the available columns in table 'testimony':
 * @property integer $_id
 * @property integer $insertTime
 * @property integer $updateTime
 * @property string $name
 * @property string $address
 * @property string $value
 */
class Testimony extends ActiveRecord {

	public $file;

	/**
	 * Returns the static model of the specified AR class.
	 * @return Testimony the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'testimony';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'file',
				'file',
				'types'=>'jpg, gif, png',
				'maxSize'=>10485760,
				'allowEmpty'=>true,
			),
			array('name, address, value', 'required'),
			array('name, address', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('name, address, value', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'_id'=>'ID',
			'insertTime'=>'Insert Time',
			'updateTime'=>'Update Time',
			'name'=>'Name',
			'address'=>'Address',
			'value'=>'Value',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('_id', $this->_id);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('address', $this->address, true);
		$criteria->compare('value', $this->value, true);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
			));
	}

	/**
	 * @return boolean
	 */
	protected function beforeSave() {
		if (!parent::beforeSave()) return false;
		$this->file = CUploadedFile::getInstance($this, 'file');
		if ($this->file) {
			$this->extension = strtolower($this->file->extensionName);
		}
		return true;
	}

	/**
	 * If scenario is insert then save the uploaded image
	 */
	protected function afterSave() {
		parent::afterSave();
		if ($this->file) {
			// We must save the uploaded file here because path depends on _id
			// and _id only exist after saving.
			$folder = Yii::app()->params['file'];
			if (!is_dir("$folder/testimonies")) {
				mkdir("$folder/testimonies");
			}
			$path = "$folder/testimonies/$this->_id.$this->extension";
			$this->file->saveAs($path);
		}
	}

	/**
	 * Delete image if exists
	 */
	protected function afterDelete() {
		$folder = Yii::app()->params['file'];
		$path = "$folder/testimonies/$this->fileName";
		parent::afterDelete();
		if (is_file($path)) unlink($path);
	}

	public function getFileName() {
		return "$this->_id.$this->extension";
	}

	public static function latestMenuItems() {
		$criteria = new CDbCriteria;
		$criteria->order = 'updateTime DESC';
		$criteria->limit = 10;
		$rows = self::model()->findAll($criteria);
		$items = array();
		foreach ($rows as $row) {
			$item = array(
				'label'=>$row->name,
				'url'=>array('testimony/view', 'id'=>$row->_id)
			);
			$items[] = $item;
		}
		return $items;
	}

}